package com.bicon.botu.mall.base.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.base.common.ValidationUtil;
import com.bicon.botu.mall.base.dto.ExpressTemplateDTO;
import com.bicon.botu.mall.base.entity.ExpressTemplate;
import com.bicon.botu.mall.base.service.IExpressTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 快递单据 前端控制器
 *
 * @author liushujie
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V3.0.0/express-template")
public class ExpressTemplateController extends BaseController {

    @Autowired
    IExpressTemplateService iExpressTemplateService;

    /**
     * 新增快递单模板
     *
     * @param name   名称
     * @param width  宽
     * @param height 高
     * @param url    图片url
     * @param sort   排序号
     * @param anchorPoint 坐标
     * @param offset 偏移量
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam("name") String name, @RequestParam("width") String width,
                             @RequestParam("height") String height, @RequestParam("url") String url,
                             @RequestParam("sort") Integer sort, @RequestParam("anchorPoint") String anchorPoint, @RequestParam("offset") String offset) {

        if (ValidationUtil.isEmpty(name)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if (ValidationUtil.isEmpty(width)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if (ValidationUtil.isEmpty(height)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if (ValidationUtil.isEmpty(url)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if (ValidationUtil.isEmpty(sort)) {
            //默认为0
            sort = 0;
        }
        if (ValidationUtil.isEmpty(anchorPoint)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if (ValidationUtil.isEmpty(offset)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        return iExpressTemplateService.insert(name, width, height, url, sort, anchorPoint,offset);
    }


    /**
     * 修改快递单模板
     *
     * @param id     主鍵id
     * @param name   名称
     * @param width  宽
     * @param height 高
     * @param url    图片url
     * @param sort   排序号
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("width") String width,
                             @RequestParam("height") String height, @RequestParam("url") String url, @RequestParam("sort") Integer sort,
                             @RequestParam("anchorPoint") String anchorPoint, @RequestParam("offset") String offset) {
        return iExpressTemplateService.update(id, name, width, height, url, sort, anchorPoint,offset);
    }

    /**
     * 逻辑删快递单模板[批量]
     *
     * @param ids 快递单模板Id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult deleteDeliveryPoint(@RequestParam("ids") String ids) {
        return iExpressTemplateService.deleteExpressTemplate(ids);
    }

    /**
     * 快递模板列表
     *
     * @param pageNo   当前页
     * @param pageSize 页码
     * @return 快递单据列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResult select(@RequestParam int pageNo, @RequestParam int pageSize) {
        Page<ExpressTemplate> expressTemplatePage = new ExpressTemplate().selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<ExpressTemplate>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("sort", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, expressTemplatePage);
    }


    /**
     * 获取已启用的快递单模板列表
     *
     * @return
     */
    @RequestMapping(value = "/enabled-list", method = RequestMethod.GET)
    public BaseResult enabled() {
        List<ExpressTemplate> expressTemplateList = new ExpressTemplate().selectList(new EntityWrapper<ExpressTemplate>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).
                eq("status", 1).orderBy("sort", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, expressTemplateList);
    }


    /**
     * 根据模板id获取模板详情
     *
     * @param id 模板ID
     * @return 模板对象
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public BaseResult detail(@RequestParam("id") String id) {
        ExpressTemplateDTO expressTemplateDTO = iExpressTemplateService.detail(id);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, expressTemplateDTO);
    }


    /***
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/update-status", method = RequestMethod.POST)
    public BaseResult updateStatus(@RequestParam("id") String id, @RequestParam("status") Integer status) {
        return iExpressTemplateService.updateStatus(id, status);
    }

}
